/*!
 Temelia - Interval tree performance tests.

 Copyright (C) 2008, 2009 Ceata (http://ceata.org/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/interval_tree.h>
}

static void null_handler(void *key, void *context)
{

}

static interval_tree_t __temelia_interval_tree_insert(int n)
{
	interval_tree_t itree;
	int i;

	itree = interval_tree_new(0, n - 1);

	for (i = 0; i < n; i++)
		interval_tree_insert(itree, i / 2, i, default_collection[i]);

	return itree;
}

static void temelia_interval_tree_insert(int n)
{
	interval_tree_delete(__temelia_interval_tree_insert(n));
}

static void temelia_interval_tree_search(int n)
{
	interval_tree_t itree;
	int i;

	itree = __temelia_interval_tree_insert(n);

	for (i = 0; i < n; i++)
		interval_tree_search(itree, i / 2, i, null_handler, NULL);

	interval_tree_delete(itree);
}

static void temelia_interval_tree_remove(int n)
{
	interval_tree_t itree;
	int i;

	itree = __temelia_interval_tree_insert(n);

	for (i = 0; i < n; i++)
		interval_tree_remove(itree, i / 2, i);

	interval_tree_delete(itree);
}

void TemeliaPerformance::pt_interval_tree(void)
{
	TestUnit testUnit("interval_tree");

	GENERATE_TEMELIA_TEST(testUnit, "insert", interval_tree_insert);
	GENERATE_TEMELIA_TEST(testUnit, "search", interval_tree_search);
	GENERATE_TEMELIA_TEST(testUnit, "remove", interval_tree_remove);

	testUnit.write(fout);
}
